//
//  SelfAddressViewController.m
//  shop
//
//  Created by 邵存将 on 17/5/31.
//  Copyright © 2017年 geek-zoo studio. All rights reserved.
//

#import "SelfAddressViewController.h"
#import "AddressDao.h"
#import <SVPullToRefresh/SVPullToRefresh.h>
#import <Masonry/Masonry.h>
#import "MyAddressTableViewCell.h"
#import "AddSelfAddressViewController.h"

@interface SelfAddressViewController ()

@end

@implementation SelfAddressViewController{
    AddressDao *addressDao;
    UITableView *_tableView;
    BOOL _isLoadingData;
    NSMutableArray *datas;
}


-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    addressDao=[[AddressDao alloc]initWithDelagate:self];

    datas=[[NSMutableArray alloc]init];
    _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self setExtraCellLineHidden:_tableView];
    datas=[[NSMutableArray alloc]init];
//    _tableView.contentInset=UIEdgeInsetsMake(64, 0, 49, 0);
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor=[UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator=NO;
    [_tableView addPullToRefreshWithActionHandler:^{
        if (!_isLoadingData) {[self reloadData];}
    }];
    [_tableView.pullToRefreshView setTitle:@"下拉刷新" forState:SVPullToRefreshStateAll];
    [_tableView.pullToRefreshView setTitle:@"松开刷新" forState:SVPullToRefreshStateTriggered];
    [_tableView.pullToRefreshView setTitle:@"正在加载" forState:SVPullToRefreshStateLoading];
    [_tableView registerNib:[UINib nibWithNibName:@"MyAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyAddressTableViewCell"];
     [self reloadData];
}



-(void)reloadData{
   [addressDao get_my_address];
}

- (void)dealloc
{
    addressDao.remoteDaoDelegate=nil;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict=datas[indexPath.row];
    MyAddressTableViewCell *addressCell= (MyAddressTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyAddressTableViewCell"];
    addressCell.selectionStyle=UITableViewCellSelectionStyleNone;
    [addressCell setContent:dict];
    [addressCell.editBtn setTag:indexPath.row];
    [addressCell.editBtn addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    [addressCell.delBtn setTag:indexPath.row];
    [addressCell.delBtn addTarget:self action:@selector(del:) forControlEvents:UIControlEventTouchUpInside];
    return addressCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict=datas[indexPath.row];
    
    [addressDao set_default_addr:[[dict objectForKey:@"address_id"]integerValue]];
}



-(IBAction)edit:(id)sender{
    NSInteger tag= [sender tag];
    NSNumber *addressId=[datas[tag] objectForKey:@"address_id"];
    AddSelfAddressViewController *addMyAddressViewController=[[AddSelfAddressViewController alloc]init];
    addMyAddressViewController.address_id=addressId;
    [self.navigationController pushViewController:addMyAddressViewController animated:YES];
}

-(IBAction)del:(id)sender{
     NSInteger tag= [sender tag];
    NSNumber *addressId=[datas[tag] objectForKey:@"address_id"];
    [addressDao del_my_address:[addressId integerValue]];
}

-(void)getRemotedata:(NSMutableDictionary*)dic{
    if ([dic objectForKey:@"get_my_address"]) {
        [_tableView.pullToRefreshView stopAnimating];
        [datas removeAllObjects];
        NSArray  *addressdatas=[[dic objectForKey:@"get_my_address"] objectForKey:@"info"];
        [datas addObjectsFromArray:addressdatas];
        [_tableView reloadData];
        
    }
    if ([dic objectForKey:@"del_my_address"]) {
        NSNumber *addressId=[dic objectForKey:@"del_my_address"] ;
        for (int i=0; i<datas.count;i++) {
            NSDictionary *dict=datas[i];
            if ([[dict objectForKey:@"address_id"] integerValue]==[addressId integerValue]) {
                [datas removeObjectAtIndex:i];
                [_tableView reloadData];
                break;
            }
        }
    }
    if ([dic objectForKey:@"set_default_addr"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }

}

//-(void)viewWillAppear:(BOOL)animated{
//    [self reloadData];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
